package com.pctx.devicemanager.controller;

import com.github.pagehelper.PageHelper;
import com.pctx.common.result.Result;
import com.pctx.devicemanager.pageHelper.PageInfo;
import com.pctx.devicemanager.pojo.DeviceUser;
import com.pctx.devicemanager.service.DeviceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chason on 2018/3/1.10:43
 */
@RestController
@RequestMapping("deviceUser")
public class DeviceUserController {
    @Autowired
    private DeviceUserService deviceUserService;

    @GetMapping("list")
    public Result list(@RequestParam String deviceUuid) throws Exception {
        PageHelper.startPage(1, 2);
        List<DeviceUser> list = deviceUserService.getByDeviceUuid(deviceUuid);
        return Result.success(list);
    }

    @GetMapping("page")
    public Result page(@RequestParam String deviceUuid, @RequestParam(value = "page", defaultValue = "1") Integer page
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<DeviceUser> list = deviceUserService.getByDeviceUuid(deviceUuid);
        return Result.success(new PageInfo<DeviceUser>(list));
    }

    @GetMapping("detail")
    public Result detail(@RequestParam String uuid) throws Exception {
        DeviceUser du = deviceUserService.getByUuid(uuid);
        return Result.success(du);
    }

    @PostMapping("add")
    public Result add(DeviceUser du) throws Exception {
        boolean flag = deviceUserService.add(du);
        if (flag)
            return Result.success("添加成功");
        else
            return Result.error("添加失败");
    }

    @PostMapping("edit")
    public Result eidt(DeviceUser du) throws Exception {
        boolean flag = deviceUserService.update(du);
        if (flag)
            return Result.success("修改成功");
        else
            return Result.error("修改失败");
    }
}
